﻿using SqlSugar;
using StackExchange.Redis;
using Standard.Licensing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TlToolBase.InterfaceMode;
using TlToolBase.Mode;

namespace TlToolBase
{
    /// <summary>
    /// 数据库帮助
    /// </summary>
    public class DatabaseHelp
    {
        IdbMode dbMode;
        public DatabaseHelp(IdbMode mode) => dbMode = mode;
        public SqlSugarClient GetSqlSugar()
        {
            //"Server=localhost;User ID=root;Password=tl123;port=3306;Database=tl_fms_db;CharSet=utf8;pooling=true;SslMode=None;"
            SqlSugarClient db=null;
            if (dbMode.type==DbType.MySql)
            {
                db = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = dbMode.type,
                    ConnectionString = string.Format("server={0};Database={1};User ID={2};Password={3};CharSet=utf8;pooling=true;SslMode=None;", dbMode.dbIp, dbMode.dbName, dbMode.dbUser, dbMode.dbPasswork),
                    IsAutoCloseConnection = true
                });
            }
            if (dbMode.type==DbType.SqlServer)
            {
                db = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = dbMode.type,
                    ConnectionString = string.Format("server={0};Database={1};Uid={2};Pwd={3}", dbMode.dbIp, dbMode.dbName, dbMode.dbUser, dbMode.dbPasswork),
                    IsAutoCloseConnection = true
                });
            }
            return db;
        }
        /// <summary>
        /// 获取视图信息
        /// </summary>
        public List<DBTableInfo> GetViewInfo()
        {
            var sql = GetSqlSugar();
            List<DBTableInfo> dBTableInfos = new List<DBTableInfo>();
            List<DbTableInfo> viewinfo = sql.DbMaintenance.GetViewInfoList(false);
            foreach (var item in viewinfo)
            {
                dBTableInfos.Add(new DBTableInfo()
                {
                    cols = GetSqlSugar().DbMaintenance.GetColumnInfosByTableName(item.Name,false),
                    viewinfo= item
                });
            }
            sql.Close();
            sql.Dispose();
            return dBTableInfos;
        }
        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tablename"></param>
        /// <param name="tableDescription"></param>
        /// <param name="classname"></param>
        public void CreateClass(string tablename, string tableDescription, string classname, Func<List<SqlSugarcColsInfoMode>> func, SqlSugarClient db = null)
        {
            if (db == null)
            {
                db = GetSqlSugar();
            }
            //db.CodeFirst.CreateClass
            var typeBilder = db.DynamicBuilder().CreateClass(classname, new SugarTable() { TableName = tablename, TableDescription = tableDescription });


            ////可以循环添加列
            //typeBilder.CreateProperty("Id", typeof(int), new SugarColumn() { IsPrimaryKey = true, IsIdentity = true });
            //typeBilder.CreateProperty("Name", typeof(string), new SugarColumn() { });
            var cols = func?.Invoke();
            foreach (var item in cols)
            {
                typeBilder.CreateProperty(item.propertyName, item.properyType, item.column);
            }

            //SugarColumn 说明看标题4

            //创建类
            var type = typeBilder.BuilderType();//想缓存有typeBilder.WithCache

            //创建表
            db.CodeFirst.InitTables(type); //建表属性API看迁移
            //db.DbFirst.IsCreateAttribute(true).CreateClassFile
        }
        /// <summary>
        /// 删除表
        /// </summary>
        public bool DeleteTable(string tablename, SqlSugarClient db = null)
        {
            if (db == null)
            {
                db = GetSqlSugar();
            }
            var lsit = GetAllTable(db);
            if (lsit.Count > 0)
            {
                if (lsit.Find(x => x.Name == tablename) != null)
                {
                    return db.DbMaintenance.DropTable(tablename);
                }
            }
            return false;
        }
        public List<DbTableInfo> GetAllTable(SqlSugarClient db = null)
        {
            if (db == null)
            {
                db = GetSqlSugar();
            }
            return db.DbMaintenance.GetTableInfoList(false);
        }

        public List<DBTableInfo> GetTableInfo()
        {
            var sql = GetSqlSugar();
            List<DBTableInfo> dBTableInfos = new List<DBTableInfo>();
            List<DbTableInfo> viewinfo = sql.DbMaintenance.GetTableInfoList(false);
            foreach (var item in viewinfo)
            {
                dBTableInfos.Add(new DBTableInfo()
                {
                    cols = GetSqlSugar().DbMaintenance.GetColumnInfosByTableName(item.Name, false),
                    viewinfo = item
                });
            }
            sql.Close();
            sql.Dispose();
            return dBTableInfos;
        }
    }

    public class DBTableInfo
    {
        public DbTableInfo viewinfo {  get; set; }
        public List<DbColumnInfo> cols {  get; set; }
    }
}

